<template>
  <div>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-house" style="font-weight: bold">&nbsp;&nbsp;后台管理</i>
        </el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-data">&nbsp;&nbsp;财务管理</i></el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-notebook-1">&nbsp;&nbsp;应收帐</i></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="Receive">
      <div class="data-canRead" style="width: 100%;background-color: white;height: auto">
        <el-divider content-position="left">信息查询</el-divider>
        <el-table :data="receivables.slice((currentPage-1)*pageSize,currentPage*pageSize)" style="width: 100%" stripe>
          <el-table-column prop="date" label="日期" width="200px"></el-table-column>
          <el-table-column prop="incomeType" label="收入类型" width="200px"></el-table-column>
          <el-table-column prop="description" label="收账-企业名" width="200px"></el-table-column>
          <el-table-column prop="tradeMethods" label="交易方式" width="200px"></el-table-column>
          <el-table-column prop="amount" label="金额" width="200px"></el-table-column>
          <el-table-column prop="remarks" label="备注" width="200px"></el-table-column>
          <el-table-column label="操作" width="200px" fixed="right">
            <template slot-scope="scope">
              <el-button
                  size="mini"
                  type="primary"
                  circle
                  @click="openEditDialog(scope.row)"><i class="el-icon-edit" style="color: white"></i>
              </el-button>
              <el-button
                  size="mini"
                  type="danger"
                  circle
                  @click="openDeleteConfirm(scope.row)"><i class="el-icon-delete" style="color: white"></i>
              </el-button>
            </template>
          </el-table-column>
          <el-table-column></el-table-column>
        </el-table>
        <div style="margin-top:15px;">
          <el-pagination align='center' @size-change="handleSizeChange"
                         @current-change="handleCurrentChange"
                         :current-page="currentPage"
                         :page-sizes="[1,5,10,20]"
                         :page-size="pageSize"
                         layout="total, sizes, prev, pager, next, jumper"
                         :total="receivables.length">
          </el-pagination>
        </div>

        <!--    修改窗口    -->
        <el-dialog
            :modal-append-to-body="false"
            title="编辑应收帐"
            width="80%"
            :visible.sync="dialogFormVisible">
          <el-form :model="editForm">
            <el-form-item label="日期" :label-width="formLabelWidth">
              <el-date-picker
                  v-model="editForm.date"
                  type="date"
                  format="yyyy 年 MM 月 dd 日"
                  value-format="yyyy-MM-dd"
                  placeholder="选择入职时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="收入类型" :label-width="formLabelWidth">
              <el-select v-model="editForm.incomeType" placeholder="请选择交易方式">
                <el-option
                    v-for="item in incomeTypes"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="摘要" :label-width="formLabelWidth">
              <el-input v-model="editForm.description" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="金额" :label-width="formLabelWidth">
              <el-input v-model="editForm.amount" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="交易方式" :label-width="formLabelWidth">
              <el-select v-model="editForm.tradeMethods" placeholder="请选择交易方式">
                <el-option
                    v-for="item in tradeMethod"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="备注" :label-width="formLabelWidth">
              <el-input v-model="editForm.remarks" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false" type="warning" style="color:white;">取 消</el-button>
            <el-button type="primary" @click="submitEditForm()" style="color:white;">确 定</el-button>
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogFormVisible: false,
      formLabelWidth: '120px',

      incomeTypes: [
        {value: "生产费用", label: "生产费用"},
        {value: "外购费用", label: "外购费用"},
        {value: "管理费用", label: "管理费用"},
        {value: "财务费用", label: "财务费用"},
        {value: "营业费用", label: "营业费用"},
      ],
      tradeMethod: [
        {
          value: "支付宝",
          label: "支付宝"
        },
        {
          value: "现金",
          label: "现金"
        },
        {
          value: "微信",
          label: "微信"
        },
        {
          value: "转账",
          label: "转账"
        },
        {
          value: "公司账号",
          label: "公司账号"
        },
      ],

      currentPage: 1,
      pageSize: 10,

      editForm: {
        date: '',
        incomeType: '',
        description: '',
        amount: '',
        tradeMethods: '',
        remarks: ''
      },
      receivables: [],
    }
  },
  methods: {
    openEditDialog(editForm) {
      let url = 'http://localhost:9080/financeReceivables/' + editForm.id;
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.dialogFormVisible = true;
          this.editForm = responseBody.data;
        } else {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadFinanceReceivablesList();
            }
          });
        }
      });
    },
    submitEditForm() {
      let url = 'http://localhost:9080/financeReceivables/' + this.editForm.id + '/update';
      console.log('url = ' + url);
      let formData = this.qs.stringify(this.editForm);
      console.log('formData = ' + formData);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url, formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '修改应收帐成功！',
            type: 'success'
          });
          this.loadFinanceReceivablesList();
          this.dialogFormVisible = false;
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadFinanceReceivablesList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    openDeleteConfirm(editForm) {
      let message = '此操作将永久删除【' + editForm.description + '】应收帐，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(editForm);
      }).catch(() => {
      });
    },
    handleDelete(editForm) {
      let url = 'http://localhost:9080/financeReceivables/' + editForm.id + '/delete';
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '删除应收帐成功！',
            type: 'success'
          });
          this.loadFinanceReceivablesList();
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadFinanceReceivablesList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    loadFinanceReceivablesList() {
      let url = 'http://localhost:9080/financeReceivables';
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        this.receivables = responseBody.data;
      });
    },
    /*表格*/
    //每页条数改变时触发 选择一页显示多少行
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.currentPage = 1;
      this.pageSize = val;
    },
    //当前页改变时触发 跳转其他页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
    },
  },
  mounted() {
    this.loadFinanceReceivablesList();
  }
}
</script>

<style scoped>
* {
  color: black;
}

.Receive {
  background-color: white;
  box-shadow: 0px 5px 5px #8d858a;
  margin-left: 0;
}

.receivablesFormSubmitSearch {
  width: 200px;
  float: left;
  right: 10px;
  background-color: white;
}

.data-canRead {
  width: 380px;
  height: 380px;
  margin-top: 15px;
  box-shadow: 0px 5px 5px #8d858a;
  float: left;
  margin-right: 16px;
}

</style>